package com.shengruan.admin.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;

import org.apache.commons.lang.StringUtils;

import com.shengruan.admin.jdbc.UserJdbc;
import com.shengruan.admin.vo.User;
import com.shengruan.common.BaseAction;
import com.shengruan.common.Constants;
import com.shengruan.common.OnLineUser;
import com.shengruan.common.OnlineCounter;
import com.shengruan.framework.pager.Pager;
import com.shengruan.framework.util.AuthFactory;

public class UserManageAction extends BaseAction {


    private UserJdbc userJdbc = (UserJdbc)this.getJdbc(UserJdbc.class);


    public void list() throws Exception {
        String userName = this.input("userName");
        int currentPage = 1;
		try {
			currentPage = Integer.parseInt(this.input("currentPage"));
		} catch(Exception ex) { }
        
		Pager pager = new Pager(currentPage,userJdbc.getRows(userName),30);
		List<User> userList = userJdbc.findWithPage(currentPage, 30,userName);

        this.setRequest("userName", userName);
		this.setRequest("pager", pager);
		this.setRequest("userList", userList);

        this.forward("/admin/user/user-list.jsp");
    }


    public void getById() throws Exception {
        String id = this.input("id");

        this.setRequest("user", userJdbc.getById(Integer.valueOf(id)));
        this.forward("/admin/user/user-edit.jsp");
    }

    public void delete() throws Exception {
        String id = this.input("id");

        userJdbc.deleteById(Integer.valueOf(id));

        this.setRequest(Constants.MESSAGE, "删除用户成功。");
        this.redirect("/admin/user/user-list.ac");
    }

    public void edit() throws Exception {
        String id = this.input("id");
        String userName = this.input("userName");
        String password = this.input("password");
        String sex = this.input("sex");
        String address = this.input("address");
        String zip = this.input("zip");
        String mobile = this.input("mobile");
        String email = this.input("email");
        String country = this.input("country");
        String city = this.input("city");
        String county = this.input("county");
        String options = this.input("options");

        User user = userJdbc.getById(Integer.valueOf(id));
        user.setUserName(userName);
        if(StringUtils.isNotEmpty(password)) {
            AuthFactory.passwordKey = Constants.passwordKey;
            user.setPassword(AuthFactory.encryptPassword(password));
        }
        if(StringUtils.isNotEmpty(sex)) user.setSex(sex);
        if(StringUtils.isNotEmpty(address)) user.setAddress(address);
        if(StringUtils.isNotEmpty(zip)) user.setZip(Integer.valueOf(zip));
        if(StringUtils.isNotEmpty(mobile)) user.setMobile(mobile);
        if(StringUtils.isNotEmpty(email)) user.setEmail(email);
        if(StringUtils.isNotEmpty(country)) user.setCountry(country);
        if(StringUtils.isNotEmpty(city)) user.setCity(city);
        if(StringUtils.isNotEmpty(county)) user.setCounty(county);
        if(StringUtils.isNotEmpty(options)) user.setOptions(options);

        userJdbc.update(user);
        this.setRequest(Constants.MESSAGE, "修改用户成功。");

        this.redirect("/admin/user/user-list.ac");
    }
    
    
	/**
	 * 获得当前在线用户列表
	 */
	public void getOnLineUserList()
	throws ServletException, IOException
	{
		List<User> userList = new ArrayList<User>();
		Map<Integer,User> onLineUsers = OnLineUser.getAllUser();
		Set<Integer> keySet = onLineUsers.keySet();
		Iterator<Integer> iterator = keySet.iterator();
		while(iterator.hasNext())
		{
			User user = onLineUsers.get(iterator.next());
			userList.add(user);
		}
		int onLineNum = onLineUsers.size();
		request.setAttribute("userList", userList);
		request.setAttribute("onLineNum",onLineNum);
		request.setAttribute("visitorNum", OnlineCounter.getUsers().size());
		request.getRequestDispatcher("/admin/user/onLineUserList.jsp").forward(request, response);
	}
}
